package com.sinosoft.dsp.platform.services;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.NoneScoped;

import com.sinosoft.builder.config.BusinessConceptHelper;
import com.sinosoft.builder.templates.concept.PrimitiveContentConceptRef;
import com.sinosoft.builder.templates.concept.PrimitiveContentConceptType;
import com.sinosoft.builder.utils.BusinessConceptDomainType;
import com.sinosoft.builder.utils.BusinessConceptTableType;
import com.sinosoft.dsp.persistence.businessconcept.facade.DomainFacade;
import com.sinosoft.dsp.persistence.businessconcept.facade.IDomainFacade;
import com.sinosoft.dsp.persistence.exception.SinoPersistenceException;
import com.sinosoft.dsp.platform.exception.SinoPlatformException;
import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.platform.manager.ApplicationBuilder;
import com.sinosoft.dsp.system.utils.ReflectionUtils;

/**
 * 业务概念模型服务类
 * 
 * @author 潘巍（Peter Pan）
 * @since 2011-4-15 下午12:09:52
 */
@ManagedBean(name = "bussinessConceptService")
@NoneScoped
public class BussinessConceptService {

	private static final Log log = Log.getLog(BussinessConceptService.class
			.getName());

	@ManagedProperty(value = "#{applicationBuilder}")
	ApplicationBuilder ab;

	/**
	 * 通过ID获得业务概念的元数据
	 * 
	 * @param id
	 * @return
	 */
	public Object getBusinessConceptMeta(String id) {
		try {
			if (ab.getBusinessConceptMetas() == null) {
				ab.setBusinessConceptMetas(BusinessConceptHelper
						.loadBusinessConceptMetas(ab.getConcept()));
			}
			return ab.getBusinessConceptMetas().get(id);
		} catch (Exception e) {
			log.error("通过ID获得业务概念的元数据出现错误(getBusinessConceptMeta)！");
		}
		return null;
	}

	/**
	 * 通过ID获得内容概念的类型
	 * 
	 * @param id
	 * @return
	 */
	public String getContentConceptType(String id) {
		try {
			Object o = getBusinessConceptMeta(id);
			if (o instanceof PrimitiveContentConceptRef) {
				PrimitiveContentConceptRef ref = (PrimitiveContentConceptRef) o;
				PrimitiveContentConceptType type = (PrimitiveContentConceptType) ref
						.getRef();
				return type.getAttributeType().value();
			}
			return null;
		} catch (Exception e) {
			log.error("通过ID获得内容概念的类型出现错误！(getContentConceptType)！");
		}
		return null;
	}

	/**
	 * 通过命名获得业务概念实体的类
	 * 
	 * @param name
	 * @return
	 */
	public Class<?> getBusinessConceptTableClass(BusinessConceptTableType type) {
		try {
			return BusinessConceptHelper.getBusinessConceptTableClass(type);
		} catch (Exception e) {
			log.error("通过命名获得业务概念实体的类出现错误！(getDomainObjectClass)！");
		}
		return null;
	}

	/**
	 * 创建空的领域代理对象
	 * 
	 * @return
	 */
	public IDomainFacade createDomainFacade() {
		try {
			return (IDomainFacade) ReflectionUtils
					.newInstance(DomainFacade.class);
		} catch (Exception e) {
			log.error("创建领域代理对象出现错误！(createDomainFacade)");
		}
		return null;
	}

	/**
	 * 通过元数据ID，创建领域代理对象
	 * 
	 * @param conceptId
	 * @return
	 */
	public IDomainFacade createDomainFacade(String conceptId) {
		try {
			Object o = getBusinessConceptMeta(conceptId);
			if (BusinessConceptDomainType.getDomainType(o) == null) {
				throw new SinoPlatformException("通过的模型元数据ID无法用来创建领域代理对象");
			}

			IDomainFacade rtn = (IDomainFacade) ReflectionUtils
					.newInstance(DomainFacade.class);
			rtn.setConceptId(conceptId);
			return rtn;
		} catch (SinoPlatformException e) {
			log.error(e.getMessage());
		} catch (Exception e) {
			log.error("创建领域代理对象出现错误！(createDomainFacade)");
		}
		return null;
	}

	/**
	 * 通过元数据ID获得领域类型<br>
	 * 如果元数据ID不是对应领域类型，返回null
	 * 
	 * @param conceptId
	 * @return
	 */
	public BusinessConceptDomainType getDomainType(String conceptId) {
		try {
			Object o = getBusinessConceptMeta(conceptId);
			return BusinessConceptDomainType.getDomainType(o);
		} catch (Exception e) {
			log.error("通过元数据ID获得领域类型出现错误！(getDomainType)");
		}
		return null;
	}

	/**
	 * 通过ID获得一个领域代理
	 * 
	 * @param id
	 * @param emName
	 * @return
	 * @throws SinoPersistenceException
	 */
	public IDomainFacade getDomainFacade(String id)
			throws SinoPersistenceException {
		try {
			IDomainFacade df = createDomainFacade();
			df.instantiate(id);
			return df;
		} catch (Exception e) {
			log.error("通过ID获得一个领域代理出现错误！(getDomainFacade)");
			throw new SinoPersistenceException(
					"通过ID获得一个领域代理出现错误！(getDomainFacade)", e);
		}
	}

}
